package com.bw.trade.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.core.creditStatus.RefundBorrowStatus;
import com.bw.trade.mapper.TbBorrowLogMapper;
import com.bw.trade.mapper.TbRefoundLogMapper;
import com.bw.trade.mapper.TbTransactionLogMapper;
import com.bw.trade.pojo.TbBorrowLog;
import com.bw.trade.pojo.TbCheckBillException;
import com.bw.trade.mapper.TbCheckBillExceptionMapper;
import com.bw.trade.pojo.TbRefoundLog;
import com.bw.trade.pojo.TbTransactionLog;
import com.bw.trade.service.TbCheckBillExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 对账异常表 服务实现类
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@Service
public class TbCheckBillExceptionServiceImpl extends ServiceImpl<TbCheckBillExceptionMapper, TbCheckBillException> implements TbCheckBillExceptionService {

    @Autowired
    private TbCheckBillExceptionMapper tbCheckBillExceptionMapper;
    @Autowired
    private TbTransactionLogMapper transactionLogMapper;
    @Autowired
    private TbBorrowLogMapper tbBorrowLogMapper;
    @Autowired
    private TbRefoundLogMapper tbRefoundLogMapper;

    //对账异常
    @Override
    public void abnormal() {

        //获取当前时间
       // Date date = new Date();
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
        LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        //查询第三方信息
        QueryWrapper<TbTransactionLog> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.apply("UNIX_TIMESTAMP(PAY_DATE) >= UNIX_TIMESTAMP('"+today_start+"')");
        queryWrapper1.apply("UNIX_TIMESTAMP(PAY_DATE) < UNIX_TIMESTAMP('"+today_end+"')");
        List<TbTransactionLog> tbTransactionLogs = transactionLogMapper.selectList(queryWrapper1);
        tbTransactionLogs.forEach(a->{
            String transactionNo = a.getTransactionNo();
            Integer transactionStatus = a.getTransactionStatus();
            BigDecimal transactionMoney = a.getTransactionMoney();
            //查询放款列表，与第三方做对比
            List<TbBorrowLog> tbBorrowLogs = tbBorrowLogMapper.selectList(null);
            tbBorrowLogs.forEach(b->{
                String outTradeNo = b.getOutTradeNo();
                Integer borrowStatus = b.getBorrowStatus();
                BigDecimal payAmount = b.getPayAmount();
                if(ObjectUtil.equal(transactionNo,outTradeNo) && !ObjectUtil.equal(transactionStatus,borrowStatus) && transactionMoney.compareTo(payAmount)!=0){
                     QueryWrapper<TbBorrowLog> queryWrapper = new QueryWrapper<>();
                     queryWrapper.eq("transactionNo",transactionNo);
                     TbBorrowLog tbBorrowLog = tbBorrowLogMapper.selectOne(queryWrapper);
                     TbCheckBillException tbCheckBillException = new TbCheckBillException();
                     tbCheckBillException.setBillNo(a.getBillNo());
                     tbCheckBillException.setBillMoney(a.getTransactionMoney());
                     tbCheckBillException.setCheckStatus(tbBorrowLog.getBorrowStatus());
                     tbCheckBillException.setCreateTime(new Date());
                     tbCheckBillException.setMsxStatus(a.getTransactionStatus());
                     tbCheckBillException.setOrderNo(tbBorrowLog.getOrderNo());
                     tbCheckBillException.setRemart(tbBorrowLog.getReturnMsg());
                     tbCheckBillException.setTransactionNo(tbBorrowLog.getTransactionNo());
                     tbCheckBillException.setTransactionStyle(1);
                     tbCheckBillException.setUserName(tbBorrowLog.getUserName());

                     tbCheckBillExceptionMapper.insert(tbCheckBillException);

                     TbBorrowLog tbBorrowLog1 = new TbBorrowLog();
                     tbBorrowLog1.setId(tbBorrowLog.getId());
                     tbBorrowLog1.setBorrowStatus(RefundBorrowStatus.REFUND_BORROW_FAIL);
                     tbBorrowLogMapper.updateById(tbBorrowLog1);

                 }
                //查询还款列表，与第三方做对比
                List<TbRefoundLog> tbRefoundLogs = tbRefoundLogMapper.selectList(null);
                 tbRefoundLogs.forEach(c->{
                     String outTradeNo1 = c.getOutTradeNo();
                     Integer refoundStatus = c.getRefoundStatus();
                     BigDecimal refoundMoney = c.getRefoundMoney();
                     if(ObjectUtil.equal(transactionNo,outTradeNo1) && !ObjectUtil.equal(transactionStatus,refoundStatus) && transactionMoney.compareTo(refoundMoney)!=0){

                         QueryWrapper<TbRefoundLog> queryWrapper = new QueryWrapper<>();
                         queryWrapper.eq("transactionNo",transactionNo);
                         TbRefoundLog tbRefoundLog = tbRefoundLogMapper.selectOne(queryWrapper);
                         TbCheckBillException tbCheckBillException = new TbCheckBillException();
                         tbCheckBillException.setBillNo(a.getBillNo());
                         tbCheckBillException.setBillMoney(a.getTransactionMoney());
                         tbCheckBillException.setCheckStatus(tbRefoundLog.getRefoundStatus());
                         tbCheckBillException.setCreateTime(new Date());
                         tbCheckBillException.setMsxStatus(a.getTransactionStatus());
                         tbCheckBillException.setOrderNo(tbRefoundLog.getOrderNo());
                         tbCheckBillException.setRemart(tbRefoundLog.getReturnMsg());
                         tbCheckBillException.setTransactionNo(tbRefoundLog.getTransactionNo());
                         tbCheckBillException.setTransactionStyle(1);
                         tbCheckBillException.setUserName(tbRefoundLog.getUserName());

                         tbCheckBillExceptionMapper.insert(tbCheckBillException);

                         TbRefoundLog tbRefoundLog1 = new TbRefoundLog();
                         tbRefoundLog1.setId(tbRefoundLog.getId());
                         tbRefoundLog1.setRefoundStatus(RefundBorrowStatus.REFUND_BORROW_FAIL);
                         tbRefoundLogMapper.updateById(tbRefoundLog1);

                     }

                     //还款放款全没记录（存）
                     QueryWrapper<TbTransactionLog> queryWrapper = new QueryWrapper<>();
                     queryWrapper.eq("transactionNo",transactionNo);
                     TbTransactionLog tbTransactionLog = transactionLogMapper.selectOne(queryWrapper);
                     TbCheckBillException tbCheckBillException = new TbCheckBillException();
                     tbCheckBillException.setBillNo(a.getBillNo());
                     tbCheckBillException.setBillMoney(a.getTransactionMoney());
                     tbCheckBillException.setCheckStatus(1);
                     tbCheckBillException.setCreateTime(new Date());
                     tbCheckBillException.setMsxStatus(a.getTransactionStatus());
                     tbCheckBillException.setTransactionStyle(1);

                     tbCheckBillExceptionMapper.insert(tbCheckBillException);

                 });
            });
        });





    }
}
